package com.lu.config;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @program lu-boot-saas
 * @description: 自定义mybatis plus 批量更新方法
 * @author: zhanglu
 * @create: 2022-09-26 14:25:00
 */
public class MyBatisPlusBatchUpdateMethod extends AbstractMethod {

  @Override
  public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass,
      TableInfo tableInfo) {
    String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>";
    String additional =
        tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.")
            : "" + tableInfo.getLogicDeleteSql(true, true);
    String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
    String sqlResult = String
        .format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(),
            "item." + tableInfo.getKeyProperty(), additional);
    //log.debug("sqlResult----->{}", sqlResult);
    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
    // 第三个参数必须和RootMapper的自定义方法名一致
    return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
  }
}
